﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChequeManagement.Common
{
    internal class PDController
    {
        //-- =============================================
        //-- Author:	    Saghari, Farid
        //-- Script Date:   1389/12/17 12:00
        //-- Description:	ApplicationInfo Class
        //-- E-Mail:    	fredmcs@live.com
        //-- Mobile:        0937-241-3499
        //-- Last Update:   1390/01/10 13:16
        //-- =============================================
        #region CorrectCodePage

        /// <summary>
        /// تصحیح (ي) و (ك) به معادل کدپیج آنها در الفبای فارسی
        /// </summary>
        /// <param name="value">رشته شامل/بدون کدپیج های عربی</param>
        /// <returns>رشته بدون کدپیج های عربی</returns>
        internal static string CorrectCodePage(string value)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(value))
                result = value.Replace('ي', 'ی').Replace('ك', 'ک');
            return (result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="businessFactory">نمونه ای از بیزینس فکتوری یک موجودیت</param>
        /// <param name="args">نام آرگومانهای یک متد از نوع آبجکت</param>
        /// <param name="parametersName">نام پارامتر های یک متد از نوع استرینگ</param>
        internal static void DoInvokeMember(object businessFactory, object[] args, string[] parametersName)
        {
            object objInstance = Activator.CreateInstance(businessFactory.GetType());
            objInstance.GetType().InvokeMember("GetAllByCondition", System.Reflection.BindingFlags.InvokeMethod, (System.Reflection.Binder)null, objInstance, args, (System.Reflection.ParameterModifier[])null, (System.Globalization.CultureInfo)null, parametersName);
        }

        #endregion

        /// -- =======================================
        /// -- Author:      Hazrati, Mahan
        /// -- Script Date: 03/24/2011 06:30:53 PM
        /// -- =======================================
        /// <summary>
        /// تبدیل حروف به اعداد که خروجی آن از نوع رشته ای میباشد
        /// </summary>
        /// <returns>تبدیل حروف به اعداد</returns>
        #region Convert Word to Number
        #region Fields
        string miliardWord = "میلیارد";
        string millionWord = "میلیون";
        string hezarWord = "هزار";
        string yekanWord = "               ";
        string result;
        string inpstring;
        string inputstringsubstring;

        int miliardLenght;
        int millionLenght;
        int hezarLenght;
        int yekanLenght;

        string miliardResult;
        string millionResult;
        string hezarResult;
        string yekanResult;

        bool miliardResultBool = false;
        bool millionResultBool = false;
        bool hezarResultBool = false;
        bool yekanResultBool = false;

        int i;

        int startposition;



        string[] yekan = { "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه" };
        string[] dahgan = { "ده", "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود" };
        string[] dahganmid = { "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده" };
        string[] sadgan = { "صد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد", };
        string javab;

        int javabsadganint;
        int javabdahganmidint;
        int javabdahganint;
        int javabyekanint;

        int sadganlenght;
        int dahganlenght;
        int yekanlenght;
        int dahganmidlenght;

        bool yekancheckbool = false;
        bool sadgancheckbool = false;
        bool dahganmidcheckbool = false;
        bool dahgancheckbool = false;

        #endregion

        #region Properties
        public string Convertwordtowumber(string inputstring, string outputStyle)
        {
            inpstring = inputstring.Trim() + yekanWord + "               ";

            miliardLenght = miliardWord.Length;
            millionLenght = millionWord.Length;
            hezarLenght = hezarWord.Length;
            yekanLenght = yekanWord.Length;

            for (i = 0; i < inpstring.Length; i++)
            {
                if (i + 15 >= inpstring.Length)
                {
                    goto check;
                }
                //check Miliard Value
                inputstringsubstring = "";
                inputstringsubstring = inpstring.Substring(i, miliardLenght);
                if (inputstringsubstring == miliardWord)
                {
                    inputstringsubstring = "";
                    inputstringsubstring = inpstring.Substring(0, i);
                    miliardResult = Convertwtn(inputstringsubstring.Trim());
                    miliardResultBool = true;
                    startposition = i + miliardLenght;
                    i = startposition;
                }
                //end check miliard value

                //check Million Value
                inputstringsubstring = "";
                inputstringsubstring = inpstring.Substring(i, millionLenght);
                if (inputstringsubstring == millionWord)
                {
                    inputstringsubstring = "";
                    inputstringsubstring = inpstring.Substring(startposition, i - startposition);
                    millionResult = Convertwtn(inputstringsubstring.Trim());
                    if (millionResult.Length == 2)
                    {
                        millionResult = "0" + millionResult;
                    }
                    if (millionResult.Length == 1)
                    {
                        millionResult = "00" + millionResult;
                    }
                    millionResultBool = true;
                    startposition = i + millionLenght;
                }
                //end check Million value
                //check hezar Value
                inputstringsubstring = "";
                inputstringsubstring = inpstring.Substring(i, hezarLenght);
                if (inputstringsubstring == hezarWord)
                {
                    inputstringsubstring = "";
                    inputstringsubstring = inpstring.Substring(startposition, i - startposition);
                    hezarResult = Convertwtn(inputstringsubstring.Trim());
                    if (hezarResult.Length == 2)
                    {
                        hezarResult = "0" + hezarResult;
                    }
                    if (hezarResult.Length == 1)
                    {
                        hezarResult = "00" + hezarResult;
                    }
                    hezarResultBool = true;
                    startposition = i + hezarLenght;
                }
                //end check hezar value

                //check yekan Value
                inputstringsubstring = "";
                inputstringsubstring = inpstring.Substring(i, yekanLenght);
                if (inputstringsubstring == yekanWord)
                {
                    inputstringsubstring = "";
                    inputstringsubstring = inpstring.Substring(startposition, i - startposition);
                    yekanResult = Convertwtn(inputstringsubstring.Trim());
                    if (yekanResult.Length == 2)
                    {
                        yekanResult = "0" + yekanResult;
                    }
                    if (yekanResult.Length == 1)
                    {
                        yekanResult = "00" + yekanResult;
                    }
                    if (yekanResult.Length == 0)
                    {
                        yekanResult = "000" + yekanResult;
                    }
                    yekanResultBool = true;
                }
                //end check yekan value


            }
        check:
            //miliard
            if (miliardResultBool == true && millionResultBool == true && hezarResultBool == true && yekanResultBool == true)
            {
                result = miliardResult + outputStyle + millionResult + outputStyle + hezarResult + outputStyle + yekanResult;
            }

            if (miliardResultBool == true && millionResultBool == true && hezarResultBool == true && yekanResultBool == false)
            {
                result = miliardResult + outputStyle + millionResult + outputStyle + hezarResult + outputStyle + "000";
            }

            if (miliardResultBool == true && millionResultBool == true && hezarResultBool == false && yekanResultBool == true)
            {
                result = miliardResult + outputStyle + millionResult + outputStyle + "000" + outputStyle + yekanResult;
            }

            if (miliardResultBool == true && millionResultBool == true && hezarResultBool == false && yekanResultBool == false)
            {
                result = miliardResult + outputStyle + millionResult + outputStyle + "000" + outputStyle + "000";
            }

            if (miliardResultBool == true && millionResultBool == false && hezarResultBool == true && yekanResultBool == true)
            {
                result = miliardResult + outputStyle + "000" + outputStyle + hezarResult + outputStyle + yekanResult;
            }

            if (miliardResultBool == true && millionResultBool == false && hezarResultBool == true && yekanResultBool == false)
            {
                result = miliardResult + outputStyle + "000" + outputStyle + hezarResult + outputStyle + "000";
            }

            if (miliardResultBool == true && millionResultBool == false && hezarResultBool == false && yekanResultBool == true)
            {
                result = miliardResult + outputStyle + "000" + outputStyle + "000" + outputStyle + yekanResult;
            }

            if (miliardResultBool == true && millionResultBool == false && hezarResultBool == false && yekanResultBool == false)
            {
                result = miliardResult + outputStyle + "000" + outputStyle + "000" + outputStyle + "000";
            }
            //million
            if (miliardResultBool == false && millionResultBool == true && hezarResultBool == true && yekanResultBool == true)
            {
                result = millionResult + outputStyle + hezarResult + outputStyle + yekanResult;
            }

            if (miliardResultBool == false && millionResultBool == true && hezarResultBool == true && yekanResultBool == false)
            {
                result = millionResult + outputStyle + hezarResult + outputStyle + "000";
            }

            if (miliardResultBool == false && millionResultBool == true && hezarResultBool == false && yekanResultBool == true)
            {
                if (yekanResult.Length > 0)
                {
                    result = millionResult + outputStyle + "000" + outputStyle + yekanResult;
                }
                if (yekanResult.Length == 0)
                {
                    result = millionResult + outputStyle + "000" + outputStyle + "000";
                }
            }

            if (miliardResultBool == false && millionResultBool == true && hezarResultBool == false && yekanResultBool == false)
            {
                if (yekanResult.Length > 0)
                {
                    result = millionResult + outputStyle + "000" + outputStyle + yekanResult;
                }
                if (yekanResult.Length == 0)
                {
                    result = millionResult + outputStyle + "000" + outputStyle + "000";
                }
            }

            //hezar
            if (miliardResultBool == false && millionResultBool == false && hezarResultBool == true && yekanResultBool == true)
            {
                result = hezarResult + outputStyle + yekanResult;
            }

            if (miliardResultBool == false && millionResultBool == false && hezarResultBool == true && yekanResultBool == false)
            {
                result = hezarResult + outputStyle + "000";
            }
            //yekan
            if (miliardResultBool == false && millionResultBool == false && hezarResultBool == false && yekanResultBool == true)
            {
                result = yekanResult;
            }



            if (result.Substring(0, 2) == "00")
            {
                result = result.Substring(2, result.Length - 2);

            }
            else if (result.Substring(0, 1) == "0")
            {
                result = result.Substring(1, result.Length - 1);
            }
            return result;
        }

        private string Convertwtn(string Inputstring)
        {

            javab = "";
            if (Inputstring != "")
            {
                Inputstring += "               ";

                yekancheckbool = false;
                sadgancheckbool = false;
                dahganmidcheckbool = false;
                dahgancheckbool = false;


                for (int i = 0; i < Inputstring.Length; i++)
                {

                    //start sadgan check


                    for (int j = 0; j <= 8; j++)
                    {

                        sadganlenght = sadgan[j].Length;

                        if (i + 15 >= Inputstring.Length)
                        {
                            goto check;
                        }
                        inputstringsubstring = Inputstring.Substring(i, sadganlenght);

                        if (inputstringsubstring == sadgan[j])
                        {
                            javabsadganint = j + 1;
                            javab = javab + javabsadganint.ToString();
                            sadgancheckbool = true;
                            i += 3 + sadgan[j].Length;
                            break;
                        }
                    }
                    //end of sadgan check

                    //start dahganmid


                    for (int d = 0; d <= 8; d++)
                    {


                        dahganmidlenght = dahganmid[d].Length;

                        inputstringsubstring = Inputstring.Substring(i, dahganmidlenght);
                        if (inputstringsubstring == dahganmid[d])
                        {
                            javabdahganmidint = d;
                            javab = javab + "1" + javabdahganmidint.ToString();
                            dahganmidcheckbool = true;
                            i += dahganmid[d].Length;
                            break;
                        }
                    }

                    //end of dahganmid
                    if (dahganmidcheckbool == false)
                    {


                        //start dahgan check

                        for (int z = 0; z <= 8; z++)
                        {

                            dahganlenght = dahgan[z].Length;

                            inputstringsubstring = Inputstring.Substring(i, dahganlenght);
                            if (inputstringsubstring == dahgan[z])
                            {
                                javabdahganint = z + 1;
                                javab = javab + javabdahganint.ToString();
                                dahgancheckbool = true;
                                i += dahgan[z].Length;
                                break;
                            }
                        }

                        //end of dahgan check


                        //start yekan check


                        for (int s = 0; s <= 8; s++)
                        {

                            yekanlenght = yekan[s].Length;

                            inputstringsubstring = Inputstring.Substring(i, yekanlenght);
                            if (inputstringsubstring == yekan[s])
                            {
                                javabyekanint = s + 1;
                                javab = javab + javabyekanint.ToString();
                                yekancheckbool = true;
                                i += 3 + yekan[s].Length;
                                break;
                            }
                        }


                        //end of yekan check
                    }

                }
            check:
                if (sadgancheckbool == true && dahgancheckbool == false && yekancheckbool == false)
                {
                    if (dahganmidcheckbool == false)
                    {
                        javab = javab + "00";
                    }
                }
                if (sadgancheckbool == false && dahgancheckbool == true && yekancheckbool == false)
                {
                    javab = javab + "0";

                }
                if (sadgancheckbool == true && dahgancheckbool == true && yekancheckbool == false)
                {
                    javab = javab + "0";

                }
                if (sadgancheckbool == true && dahgancheckbool == false && yekancheckbool == true)
                {
                    javab = javab.Substring(0, 1) + "0" + javab.Substring(javab.Length - 1, 1);
                }
            }
            return javab;
        }
        #endregion
        #endregion


    }
}
